package com.scalar.chiptrack.operations.dataentry.actions;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.dataentry.forms.OperationCodeEntryForm;
import com.scalar.chiptrack.operations.dataentry.dao.OperationCodeDAO;
import com.scalar.chiptrack.ChipTrackAction;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.ArrayList;

/**
 * Created by Sudhakar.
 * User: Administrator
 * Date: Oct 4, 2005
 * Time: 3:43:51 PM

 */
public class OperationCodeListAction extends ChipTrackAction
{

    private static final String ACTION_NAME = "/operationCodeList.do";

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws Exception
    {


        HttpSession session = request.getSession();
        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if ( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME );
            return ( mapping.findForward( "login" ) );
        }

        HashMap errors          = new LinkedHashMap();
        HashMap info            = new LinkedHashMap();
        String action           = request.getParameter( "action" );
        String actionForward    = "success";

        OperationCodeEntryForm operationCodeEntryForm = ( OperationCodeEntryForm ) form;
        //OperationCodeDAO operationCodeDAO = new OperationCodeDAO();

        if ( action == null )
        {
            action = "";
        }

        ArrayList oprCodesList  = OperationCodeDAO.loadOperationCodes( null );
        ArrayList processList = getProcessList();
        request.setAttribute("processList", processList );
        if( oprCodesList != null && oprCodesList.size() > 0 )
        {
            operationCodeEntryForm.setOprCodeList( oprCodesList );
        }
        request.setAttribute( "oprCodesList", oprCodesList );

        if ( action.equalsIgnoreCase( "view" ) )
        {
           String oprCode   = request.getParameter("oprCode").trim();
           oprCodesList     = OperationCodeDAO.loadOperationCodes( oprCode );
           operationCodeEntryForm.setOprCodeList( oprCodesList);
           actionForward = "view";
           request.setAttribute("action", action);
        }
        if ( action.equalsIgnoreCase( "modify" ) )
        {
           String oprCode   = request.getParameter("oprCode").trim();
           oprCodesList     = OperationCodeDAO.loadOperationCodes( oprCode );
           operationCodeEntryForm.setOprCodeList( oprCodesList);
           actionForward = "modify";
           request.setAttribute("action", action);
        }
        if ( action.equalsIgnoreCase( "delete" ) )
        {
            String oprCode  = request.getParameter("oprCode").trim();
            oprCodesList    = OperationCodeDAO.loadOperationCodes( oprCode );
            operationCodeEntryForm.setOprCodeList( oprCodesList);
            actionForward = "view";
            request.setAttribute("action", "confirm");
        }
        if ( action.equalsIgnoreCase( "confirm" ) )
        {
           try
            {
                String operationCode = request.getParameter("oprCode").trim();
                OperationCodeDAO.deleteOperationCode( operationCode );
                info.put( "Data has been updated successfully", "" );
                actionForward = "saveSuccess";
                request.setAttribute( SessionKeys.INFO_MESSAGES, info );
            }
            catch( Exception e )
            {
                e.printStackTrace();
                errors.put( "Error storing the Operation Code information in to Database", null );
                request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            }
        }
        if ( action.equalsIgnoreCase( "save" ) )
        {

            String oprCode = request.getParameter("oprCode").trim();
            String oprCodeDesc = request.getParameter("oprCodeDesc").trim();
            String process = request.getParameter("process").trim();
            request.setAttribute("action", action);
            try
            {
            	OperationCodeDAO.modifyOperationCode( oprCode, oprCodeDesc, process );
                info.put( "Data has been updated successfully", "" );
                actionForward = "saveSuccess";
                request.setAttribute( SessionKeys.INFO_MESSAGES, info );

            }
            catch( Exception e)
            {
                e.printStackTrace();
            }
        }
        return ( mapping.findForward( actionForward ) );

    }

    /**
     * Returns an arraylist of processes
     *
     * @return
     */
    public ArrayList getProcessList()
    {
        ArrayList categoryList = new ArrayList();
		categoryList.add( new org.apache.struts.util.LabelValueBean( "Assembly", "Assembly" ) );
        categoryList.add( new org.apache.struts.util.LabelValueBean( "Test", "Test" ) );
        return categoryList;
    }

}
